<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="报价单号" prop="productNum">
        <el-input
          v-model="queryParams.bjdH"
          placeholder="报价单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否生成合同" prop="schtbj" label-width="120">
        <el-select v-model="queryParams.schtbj" placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="创建日期" prop="createDate">
        <el-date-picker
          v-model="queryParams.createDate"
          type="date"
          placeholder="选择日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['sales:price:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          :disabled="multiple"
          @click="handleScht"
        >报价成功</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="priceList" @selection-change="handleSelectionChange" :row-class-name="tableRowClassName">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="公司名称" align="center" prop="factoryNo" />
      <el-table-column label="报价单号" align="center" prop="bjdH" width="140" />
      <el-table-column label="规格型号" align="center" prop="specification" width="200" show-overflow-tooltip/>
      <el-table-column label="客户名称" align="center" prop="unit" width="120"/>
      <el-table-column label="外径" align="center" prop="outDia" />
      <el-table-column label="内径" align="center" prop="innerDia" />
      <el-table-column label="法兰厚度" align="center" prop="flThick" />
<!--      <el-table-column label="螺栓孔径" align="center" prop="lskj" />-->
<!--      <el-table-column label="螺栓孔数量" align="center" prop="lskjNum" />-->
<!--      <el-table-column label="凸台外径" align="center" prop="ttWj" />-->
<!--      <el-table-column label="凸台内径" align="center" prop="ttNj" />-->
<!--      <el-table-column label="凸台厚度" align="center" prop="ttHd" />-->
<!--      <el-table-column label="计算体积" align="center" prop="jstj" />-->
<!--      <el-table-column label="计算重量" align="center" prop="jszl" />-->
<!--      <el-table-column label="标准重量" align="center" prop="bzsl" />-->
<!--      <el-table-column label="材料单价" align="center" prop="cldj" />-->
<!--      <el-table-column label="材料成本价" align="center" prop="clcbj" />-->
<!--      <el-table-column label="加工成本价" align="center" prop="jgcbj" />-->
<!--      <el-table-column label="密封槽成本价" align="center" prop="mfccbj" />-->
<!--      <el-table-column label="订单数量系数" align="center" prop="ddslxs" />-->
<!--      <el-table-column label="加工难度系数" align="center" prop="jgndxs" />-->
      <el-table-column label="合计成本价(元)" align="center" prop="hjcbj" width="120"/>
<!--      <el-table-column label="调整系数" align="center" prop="tzxs" />-->
<!--      <el-table-column label="理论报价" align="center" prop="llbj" />-->
      <el-table-column label="实际报价(元)" align="center" prop="sjbj" width="120" />
<!--      <el-table-column label="税率" align="center" prop="sjbj" />-->
      <el-table-column label="含税报价(元)" align="center" prop="hsbj" width="120" />
      <el-table-column label="报价日期" align="center" prop="bjDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.bjDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="报价人" align="center" prop="bjr" />
<!--      <el-table-column label="批准人" align="center" prop="pzr" />-->
      <el-table-column label="报价有效期" align="center" prop="bjyxsj" width="100"/>
      <el-table-column label="创建人" align="center" prop="createBy" width="100"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="160"/>
      <el-table-column label="报价有效性标记" align="center" prop="bjyxsjbj" width="140" >
        <template slot-scope="scope">
          <div v-if="scope.row.bjyxsjbj=='0'">
            无效
          </div>
          <div v-if="scope.row.bjyxsjbj=='1'">
            有效
          </div>
        </template>
      </el-table-column>
      <el-table-column label="是否生成合同" align="center" prop="schtbj" width="140">
      <template slot-scope="scope">
        <div style="color: #13ce66" v-if="scope.row.schtbj=='0'">
          未生成
        </div>
        <div style="color: red" v-if="scope.row.schtbj=='1'">
          已生成
        </div>
      </template>
    </el-table-column>
      <el-table-column label="是否含税" align="center" prop="isHs" width="180" fixed="right" >
        <template slot-scope="scope">
          <el-radio v-model="scope.row.isHs" label="1" @input="handlHs(scope.row)">含税</el-radio>
          <el-radio v-model="scope.row.isHs" label="0" @input="handlHs(scope.row)">不含税</el-radio>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改报价单对话框 -->
  </div>
</template>

<script>
import {listPrice, getPrice, delPrice, addPrice, updatePrice, genScht, listRules, updatIshs} from "@/api/sales/price";
import moment from "moment/moment";


export default {
  name: "Price",
  data() {
    return {
      options: [{
        value: '',
        label: '全部'
      },{
        value: '0',
        label: '未生成'
      }, {
        value: '1',
        label: '已生成'
      }],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 报价单表格数据
      priceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        factoryNo: null,
        productNo: null,
        productZhNo: null,
        productNum: null,
        productSubNum: null,
        productName: null,
        productBarcode: null,
        materialCategory: null,
        specification: null,
        unit: null,
        deviceNo: null,
        devicePower: null,
        outDia: null,
        innerDia: null,
        flThick: null,
        lskj: null,
        lskjNum: null,
        ttWj: null,
        ttNj: null,
        ttHd: null,
        jstj: null,
        jszl: null,
        bzsl: null,
        cldj: null,
        clcbj: null,
        jgcbj: null,
        mfccbj: null,
        ddslxs: null,
        jgndxs: null,
        hjcbj: null,
        tzxs: null,
        llbj: null,
        sjbj: null,
        accountDate: null,
        bjDate: null,
        bjr: null,
        pzr: null,
        bjyxsj: null,
        bjyxsjbj: null,
        schtbj: '0',
        createDate:new Date
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询报价单列表 */
    getList() {
      this.loading = true;
      this.queryParams.createDate = moment.utc(this.queryParams.createDate).local().format('YYYY-MM-DD')
      if(this.queryParams.createDate == 'Invalid date'){
        this.queryParams.createDate = null
      }
      listPrice(this.queryParams).then(response => {
        this.priceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    tableRowClassName({row, rowIndex}) {
      if (row.schtbj === '0') {
        return 'warning-row';
      } else if(row.schtbj === '1'){
        return 'success-row';
      }
      return '';
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        factoryNo: null,
        productNo: null,
        productZhNo: null,
        productNum: null,
        productSubNum: null,
        productName: null,
        productBarcode: null,
        materialCategory: null,
        specification: null,
        unit: null,
        deviceNo: null,
        devicePower: null,
        outDia: null,
        innerDia: null,
        flThick: null,
        lskj: null,
        lskjNum: null,
        ttWj: null,
        ttNj: null,
        ttHd: null,
        jstj: null,
        jszl: null,
        bzsl: null,
        cldj: null,
        clcbj: null,
        jgcbj: null,
        mfccbj: null,
        ddslxs: null,
        jgndxs: null,
        hjcbj: null,
        tzxs: null,
        llbj: null,
        sjbj: null,
        accountDate: null,
        bjDate: null,
        bjr: null,
        pzr: null,
        bjyxsj: null,
        bjyxsjbj: null,
        schtbj: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handlHs(row){
      let param ={
        id : parseInt(row.id),
        isHs : row.isHs
      }
      updatIshs(param).then(res=>{
        console.log(res,"resddddd")
        if(res.code==200){
          this.getList()
          this.$modal.msgSuccess("修改成功！");
        }else{
          this.getList()
          this.$modal.msgSuccess("修改失败！");
        }
      });
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      // for(let i=0;i<selection.length;i++){
      //   console.log(selection[i])
      //   if(selection[i].aa==''||selection[i].aa==null||selection[i].aa==undefined){
      //     selection[i].aa="1"
      //   }
      // }
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加报价单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPrice(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改报价单";
      });
    },
    /** 生成合同 */
    handleScht(row){
      this.reset();
      const id = row.id || this.ids
      genScht(id).then(response => {
        this.getList();
        this.$modal.msgSuccess(response.msg);
        this.$router.push('/sale/htgl/htcjysp')
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePrice(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPrice(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除报价单编号为"' + ids + '"的数据项？').then(function() {
        return delPrice(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('sales/price/export', {
        ...this.queryParams
      }, `price_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style>
.el-table .warning-row {
  background: oldlace;
}

.el-table .success-row {
  background: #ffe4e1;
}
</style>
